package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.User;
import com.itheima.service.MenuService;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;
    @Reference
    private UserService userService;

    @RequestMapping("/getMenus")
    public Result getMenus(@RequestParam("username") String username) {
        try {

            User user = userService.findByUsername(username);
            Integer userId = user.getId();

            List<Map> list = menuService.getMenus(userId);
            return new Result(true, MessageConstant.GET_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    /**
     * 菜单管理中：分页查询菜单
     */
    @RequestMapping("/findMenuByPage")
    public Result findMenuByPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult result = menuService.findMenuByPage(queryPageBean);
            return new Result(true, "查询成功", result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /**
     * 菜单管理中：编辑菜单
     */
    @RequestMapping("/updateMenu")
    public Result updateMenu(@RequestBody Menu menu) {
        try {
            menuService.updateMenu(menu);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }
    }

    /**
     * 添加权限
     */

    @RequestMapping("/addMenu")
    public Result addMenu(@RequestBody Menu menu) {
        try {
            menuService.addMenu(menu);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    /**
     * 删除权限
     */
    @RequestMapping("/deleteMenuById")
    public Result deleteMenuById(@RequestParam("id") Integer id) {
        try {
            menuService.deleteMenuById(id);
            return new Result(true, "删除成功");
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Menu> list = menuService.findAll();
            return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_MENU_FAIL);
        }
    }

}
